import fetch from 'dva/fetch';
//基本路径
const BASE_URL = 'http://127.0.0.1:7001';
export default function (url, options) {
    //从本地获取token 
  let token = localStorage.getItem('token');
  //如果token 存在，赋值
  if (token) {
    options.headers = options.headers || {};
    options.headers.authorization = token;
  }
  //如果不存在，就重新构造赋值，请求方法，请求头
  options.method = options.method || 'GET';     //请求方法
  options.headers["Content-Type"] = "application/json";  //HTTP的请求头
  options.headers["Accept"] = "application/json";
  options.credentials = 'include';  // 是否携带cookie，默认为omit,不携带; same-origi,同源携带; include,同源跨域都携带
  return fetch(BASE_URL + url, {
    ...baseOptions,
    ...options,
    headers: {
      ...baseOptions.headers,
      ...options.headers
    }
  }).then(res => res.json());
}